package com.ruoyi.web.config;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInterceptor;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.session.SqlSessionFactory;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import java.util.Properties;

@Configuration
public class PageHelperConfig {

        // MySQL 数据源配置
        @Bean(name = "mysqlDataSource")
        public DataSource mysqlDataSource() {
            HikariDataSource dataSource = new HikariDataSource();
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/wms?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8");
            dataSource.setUsername("root");
            dataSource.setPassword("root");
            dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            return dataSource;
        }

        // SQL Server 数据源配置
        @Bean(name = "sqlServerDataSource")
        public DataSource sqlServerDataSource() {
            HikariDataSource dataSource = new HikariDataSource();
            dataSource.setJdbcUrl("jdbc:sqlserver://172.66.9.13:1433;databaseName=ICSWMS_KBSCS");
            dataSource.setUsername("sa");
            dataSource.setPassword("Drcoffee201609");
            dataSource.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            return dataSource;
        }

        // MySQL 的 SqlSessionFactory 配置
        @Bean(name = "mysqlSqlSessionFactory")
        public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);

            // 配置 PageHelper 插件（MySQL 方言）
            PageInterceptor pageInterceptor = new PageInterceptor();
            Properties properties = new Properties();
//            properties.setProperty("dialect", "mysql"); // 使用 MySQL 方言
            properties.setProperty("rowBoundsWithCount", "true");  // 通过 RowBounds 进行分页查询时，自动执行 count 查询
            properties.setProperty("reasonable", "true");         // 设置为 true 使得页码 <= 0 时查询第一页，页码 > pagesize 时查询最后一页
            pageInterceptor.setProperties(properties);
            // 注册 PageHelper 插件
            factoryBean.setPlugins(new Interceptor[]{pageInterceptor});
            return factoryBean.getObject();
        }

        // SQLServer 的 SqlSessionFactory 配置
        @Primary
        @Bean(name = "sqlServerSqlSessionFactory")
        public SqlSessionFactory sqlServerSqlSessionFactory(@Qualifier("sqlServerDataSource") DataSource dataSource) throws Exception {
            SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
            factoryBean.setDataSource(dataSource);

            // 配置 PageHelper 插件（SQL Server 方言）
            PageInterceptor pageInterceptor = new PageInterceptor();
            Properties properties = new Properties();
//            properties.setProperty("dialect", "sqlserver"); // 使用 SQL Server 方言
            properties.setProperty("rowBoundsWithCount", "true");  // 通过 RowBounds 进行分页查询时，自动执行 count 查询
            properties.setProperty("reasonable", "true");         // 设置为 true 使得页码 <= 0 时查询第一页，页码 > pagesize 时查询最后一页
            pageInterceptor.setProperties(properties);

            // 注册 PageHelper 插件
            factoryBean.setPlugins(new Interceptor[]{pageInterceptor});
            return factoryBean.getObject();
        }
    }

